package action

import (
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/session"
	"teamcoop/global"
	. "teamcoop/models"
)

func CommentTodoAdd(sid string, tid int64, text string) (cid int64, err error) {
	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("登录账户不存在!")
		return
	}

	todo := &Todo{Id: tid}
	err = o.QueryTable("todo").Filter("Id", tid).One(&todo)
	if err != nil {
		err = errors.New("查询TODO失败!")
		return
	}
	comment := &Comment{
		UserId: user.Id,
		TodoId: todo.Id,
		Text:   text,
		Type:   TYPE_TODO,
	}
	_, err = o.Insert(comment)
	if err != nil {
		err = errors.New("添加TODO评论失败!")
		return
	}
	cid = comment.Id
	err = DynamicAddComment(sid, cid)
	return
}
func CommentAnnounceAdd(sid string, aid int64, text string) (cid int64, err error) {
	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("登录账户不存在!")
		return
	}

	announce := &Announce{Id: aid}
	err = o.QueryTable("announce").Filter("Id", aid).One(&announce)
	if err != nil {
		err = errors.New("查询TODO失败!")
		return
	}

	comment := &Comment{
		UserId:     user.Id,
		AnnounceId: announce.Id,
		Text:       text,
		Type:       TYPE_ANNOUNCE,
	}
	_, err = o.Insert(comment)
	if err != nil {
		err = errors.New("添加TODO评论失败!")
		return
	}
	cid = comment.Id
	err = DynamicAddComment(sid, cid)
	return
}
func CommentRemove(sid string, cid int64) (err error) {
	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("登录账户不存在!")
		return
	}

	comment := &Comment{Id: cid}
	err = o.QueryTable("comment").Filter("Id", cid).One(&comment)
	if err != nil {
		err = errors.New("评论不存在!")
		return
	}
	dynamic := &Dynamic{}
	err = o.QueryTable("dynamic").Filter("Type", TYPE_COMMENT).Filter("CommentId", cid).One(dynamic)
	if err != nil {
		beego.Error(err.Error())
		err = errors.New(" query  dynamic  fail!")
		return
	}
	_, err = o.Delete(dynamic)
	if err != nil {
		err = errors.New("删除动态失败!")
		return
	}
	_, err = o.Delete(comment)
	if err != nil {
		err = errors.New("删除评论失败!")
		return
	}
	return
}

func CommentListAnnounce(sid string, aid int64) (ret_list []*Comment, err error) {
	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("登录账户不存在!")
		return
	}
	announce := &Announce{Id: aid}
	o.Read(announce)
	qs = o.QueryTable("comment")
	_, err = qs.Filter("Announce", announce).All(&ret_list)
	return
}

func CommentListTodo(sid string, tid int64) (ret_list []*Comment, err error) {
	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)
	if err != nil {
		return
	}

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state {
		return
	}

	email := sessions.Get("login_email").(string)

	o := orm.NewOrm()
	user := &User{}
	qs := o.QueryTable("user")
	err = qs.Filter("email", email).One(user)
	if err != nil {
		err = errors.New("登录账户不存在!")
		return
	}
	todo := &Todo{Id: tid}
	o.Read(todo)
	qs = o.QueryTable("comment")
	_, err = qs.Filter("Todo", todo).All(&ret_list)
	return
}
